import { MVCS } from "../../core/MVCS";
import EventSystem from "../../core/EventSystem";

/**
 * fairyGUI MVC拓展类
 * @author cls
 * @date 2020-01-20
 */
export namespace FMVCS {
    export abstract class FView extends fgui.GComponent {
        /** 资源 */
        protected _package: { [path: string]: string } = {};
        /** ui控件 */
        protected _views: {[key: string]: fgui.GComponent} = null;
        /** 点击事件注册 */
        abstract clickHandlers(): (fairygui.GComponent | this | ((data: any) => void))[][];
        /** 关闭回调 */
        closeHandler: Function = () => {};

        /** view实例化完毕回调 */
        onOpenCallBack(){}

        onConstruct(){
            super.onConstruct();
            this._loadAllObject(this);
            this.rigisterClickEvent();

            this.onOpenCallBack();
        }

        rigisterClickEvent(){
            let handlers = this.clickHandlers();
            for (let i = 0; i < handlers.length; ++i){
                let info = handlers[i];
                if (info){
                    (info[0] as fgui.GComponent).onClick((info[1] as (data: any) => any), info[2]);
                }
            }
        }

        private _loadAllObject(root: fgui.GComponent, path: string = ``) {
            if (!root._children) return;
            if (!this._views){
                this._views = {};
            }
            for (let i = 0; i < root._children.length; ++i) {
                this._views[path + root._children[i].name] = root._children[i].asCom;
                this._loadAllObject(root._children[i].asCom, path + root._children[i].name + `/`);
            }
        }

        onClose(){
            this.closeHandler(this.name);
        }

        dispose(){
            EventSystem.offTarget(this);
            super.dispose();
        }
    }

    export abstract class FCtrl extends MVCS.Ctrl { 
        constructor(){
            super();
            this.rigisterEventListener();
        }
    }

    export abstract class FData extends MVCS.Data { 

    }

    export abstract class FVO extends MVCS.VO { 
        
    }
}
